package com.r2coding.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.r2coding.article.annotation.SysLog;
import com.r2coding.article.dao.ArticleDao;
import com.r2coding.article.dao.ArticleLabelDao;
import com.r2coding.article.dao.CategoryDao;
import com.r2coding.article.entity.ArticleEntity;
import com.r2coding.article.entity.ArticleLabelEntity;
import com.r2coding.article.entity.CategoryEntity;
import com.r2coding.article.vo.req.GetArticlesByIdReqVo;
import com.r2coding.article.vo.req.LabelListReqVo;
import com.r2coding.article.vo.resp.ArticleRespVo;
import com.r2coding.article.vo.resp.CategoryRespVo;
import com.r2coding.article.vo.resp.LabelListRespVo;
import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Query;

import com.r2coding.article.dao.LabelDao;
import com.r2coding.article.entity.LabelEntity;
import com.r2coding.article.service.LabelService;
import org.springframework.util.CollectionUtils;

/**
 * 
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-13 19:34:16
 */
@Service("labelService")
public class LabelServiceImpl extends ServiceImpl<LabelDao, LabelEntity> implements LabelService {

    @Autowired
    ArticleLabelDao articleLabelDao;

    @Autowired
    ArticleDao articleDao;
    
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LabelEntity> page = this.page(
                new Query<LabelEntity>().getPage(params),
                new QueryWrapper<LabelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<LabelEntity> getLabelsByArticleId(Integer id) {
        QueryWrapper<ArticleLabelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", id);
        List<ArticleLabelEntity> articleLabelEntities = articleLabelDao.selectList(wrapper);
        List<LabelEntity> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(articleLabelEntities)) {
            list = articleLabelEntities.stream().map(item -> this.baseMapper.selectById(item.getLabelId())).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<LabelListRespVo> labelList() {
        List<LabelEntity> labels = this.baseMapper.selectList(new QueryWrapper<LabelEntity>());
        List<LabelListRespVo> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(labels)) {
            list = labels.stream().map(item -> {
                QueryWrapper<ArticleLabelEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("label_id", item.getLabelId());
                Integer integer = articleLabelDao.selectCount(wrapper);
                System.out.println("labelId=" + item.getLabelId() + ", count= " + integer);
                LabelListRespVo labelListRespVo = new LabelListRespVo();
                labelListRespVo.setCount(integer);
                labelListRespVo.setLabel(item);
                return labelListRespVo;
            }).collect(Collectors.toList());
        }
        return list;
    }

    @Cacheable(value = "labelListReqVo", key = " 'LABEL_LIST' ")
    @Override
    public List<LabelListReqVo> selectAllLabels() {
        return this.baseMapper.selectAllLabels();
    }

    @Override
    public LabelListReqVo getLabelById(Integer labelId) {
        return this.baseMapper.getLabelById(labelId);
    }

    @SysLog(module = Module.ARTICLE, operationType = OperationType.INSERT, description = "添加标签")
    @Override
    public void addLabel(LabelListReqVo reqVo) {
        LabelEntity label = new LabelEntity();
        BeanUtils.copyProperties(reqVo, label);
        label.setUpdateTime(new Date());
        label.setCreateTime(new Date());
        label.setDeleted(0);
        this.baseMapper.insert(label);
    }

    @SysLog(module = Module.ARTICLE, operationType = OperationType.UPDATE, description = "更新标签")
    @Override
    public void updateLabel(LabelListReqVo reqVo) {
        // 根据标签id查询该标签信息
        LabelEntity label = this.baseMapper.selectById(reqVo.getLabelId());
        label.setUpdateTime(new Date());
        label.setCategoryId(reqVo.getCategoryId());
        label.setName(reqVo.getName());
        this.baseMapper.updateById(label);
    }

    @Override
    public ArticleRespVo getArticlesById(GetArticlesByIdReqVo vo) {
        List<ArticleEntity> list = articleDao.selectArticleByLabelId(vo.getLabelId());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        ArticleRespVo respVo = new ArticleRespVo();
        respVo.setSize(list.size());

        PageHelper.startPage(vo.getCurrent(), vo.getSize());
        List<ArticleEntity> articles = articleDao.selectArticleByLabelId(vo.getLabelId());
        List<CategoryRespVo> collect = articles.stream().map(item -> {
            CategoryRespVo categoryRespVo = new CategoryRespVo();
            BeanUtils.copyProperties(item, categoryRespVo);
            if (categoryRespVo.getTitle().length() > 10) {
                String s = categoryRespVo.getTitle().substring(0, 10);
                categoryRespVo.setTitle(s + "...");
            }
            // 根据分类id查询分类信息
            CategoryEntity category = categoryDao.selectById(item.getCategoryId());
            categoryRespVo.setCategoryName(category.getName());
            return categoryRespVo;
        }).collect(Collectors.toList());
        respVo.setRespVoList(collect);
        return respVo;
    }

    @Override
    public List<LabelEntity> getLabel() {
        return this.baseMapper.getLabel();
    }
}